<?php
class Interviews_Model extends Model {
  	
  	function __construct()
  	{
  		parent::Model();
  	}
  	function get_interview_details($id="",$limit="",$order="",$count=false)
  		{
  			if($id!="")
  	  			$this->db->where('int_goss.id', $id);
  	  		if($order!="")
  	 			$this->db->order_by('int_goss.insert_date','desc');
  	 		if($count==false)
  	 		{
  	  			if(isset($limit) && $limit>=0 )
  	   			{
  	  				$this->db->limit(9,$limit);
  	    		} 
  	 		}
  	 		$this->db->select('*');
  	   		$this->db->where('int_goss.type', 'Interview');
  	 		$this->db->from('int_goss');
			$this->db->join('int_goss_media', 'int_goss_media.int_goss_id=int_goss.id' );
  	 		$query = $this->db->get_where();
  	 		
  	 	if($count==true)
    			return $query->num_rows();
      	else
			return $query->result();
  	  }
  	function get_interview_fulldetails($id="",$limit="",$order="")
  		{
  			if($id!="")
  	  			$this->db->where('id', $id);
  	  		if($order!="")
  				 $this->db->order_by('insert_date','desc');
  	  		if($limit!="")
  	    	{
  	  			$this->db->limit($limit);
  	  	 	} 
  			$query = $this->db->get_where('int_goss',array('type'=>'Interview'));
  	 		return $query->result();
  		}
  	function get_gossips_details($id="",$limit="",$order="",$count=false,$start=0)
  		{
  		if($id!="")
  	  		$this->db->where('id', $id);
  	  	if($order!="")
  			 $this->db->order_by('insert_date','desc');
  		if($count==false)
  		 {
  	  		if(isset($limit) && $limit>=0)
  	    	{
  	  			$this->db->limit($start,$limit);
  	    	} 
   		}
  	    $this->db->where(array('type'=>'Gossips'));
  		$query = $this->db->get('int_goss');
  		if($count==true)
  			return $query->num_rows();
    	else
			return $query->result();
  		}
  	function interview_details($id){
  		$this->db->select('*');
  		$this->db->from('int_goss');
  		$this->db->join('int_goss_media','int_goss_media.int_goss_id=int_goss.id');
 		$array=array('int_goss.id'=>$id);
  		$details=$this->db->get_where('',$array);
  		return $details->result();
  	}
  	function gossip_details($id){
  		 $query=$this->db->get_where('int_goss',array('id'=>$id));
  		 return $query->result();
  	}
  	function delete_interviews($id){
  		$this->db->where('id',$id);
  		$result = $this->db->get('int_goss');
  		$rows = $result->row();
  		if($rows->type == 'Interview')
  		{
  			$path1 = "./assets/images/interviews/int".$id.".jpg";
  			$path2 = "./assets/images/interviews/int".$id."_thumb.jpg";
  		}
  		if($rows->type == 'Gossips')
  		{
  			$path1 = "./assets/images/gossips/int".$id.".jpg";
  			$path2 = "./assets/images/gossips/int".$id."_thumb.jpg";
  		}
  		$this->db->where('id',$id);
  		if($this->db->delete('int_goss'))
  		{
  			if(file_exists($path1))
  			unlink($path1);
  			if(file_exists($path2))
  			unlink($path2);	
  			return 'Deleted Successfully';
  		}
  		else 
  		return 'error';
  	}
  	function change_active($id="",$active_id=""){
  		if($active_id==0){
  			$active=1;
  		}else{
  			$active=0;
  		}
  		$data=array('active'=>$active);
		$this->db->where('id', $id);
		$this->db->update('int_goss', $data);
  	}
	function rename($details,$preview_image_path,$file_type,$id){
			
	 		if($_POST['brose']=='Image'){
	 			
			if($_POST['media']=='Interview'){
				
				$oldname="assets/images/interviews/".$details['upload_data']['file_name'];
				$result=rename($oldname,'assets/images/interviews/int'.$id.'.jpg');
			}else if($_POST['media']=='Gossips'){

					$oldname="assets/images/gossips/".$details['upload_data']['file_name'];
					$result=rename($oldname,'assets/images/gossips/int'.$id.'.jpg');
			}
	 		}
	 	 	if($_POST['brose']=='Video'){
			$oldname1=$preview_image_path.$details['upload_data']['file_name'];
			$result=rename($oldname1,$preview_image_path.'int'.$id.$file_type);
	 	 	}
			return $result;
	}
	function message($messageid)
	{
		if($messageid!=0)
		{
			if($messageid==1)
			{
				$message = 'U have Upload successfully<br> ';

			}
		if($messageid==2)
			{
				$message = 'Error In Uploading ';

			}
			return $message;
		}
		
	}

	function do_edit_update(){
		$media = $this->input->post('media');
		$person_name = $this->input->post('username');
		$description = $this->input->post('description');
		$hidden = $this->input->post('hid');
		$active = $this->input->post('active');
		$date = $this->input->post('date');
		 $id=$hidden;
		
		$data=array
			  		(
  		 			'person_name'=> $person_name,
   					'description'=>$description,
  		 			'on_date'=>$date,
 					'type'=>$media,
			  		'active' => $active,	  		
  					 );
		$this->db->where('id', $id);
		$result=$this->db->update('int_goss', $data); 
		return $result;	
	}
	function get_interview_videos(){
			$this->db->select('*');
  	   		$this->db->where('int_goss_media.active_video', '1');
  	 		$this->db->from('int_goss');
			$this->db->join('int_goss_media', 'int_goss_media.int_goss_id=int_goss.id');
  	 		$query = $this->db->get_where();
  	 		return $query->result();
	}
	function get_home_gossips()
	{
		$this->db->select('*');
		$this->db->limit(5);
		$this->db->where('type','Gossips');
		$this->db->order_by('id','desc');
		$this->db->get('int_goss');
	}
}
 ?>